#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    // 创建菜单项
    //QMenuBar* menuBar = new QMenuBar();
    QMenuBar* menuBar = this->menuBar();
    this->setMenuBar(menuBar);

    // 创建菜单, &+按键，相当于添加快捷键
    QMenu* menu1 = new QMenu("文件 (&F)");
    QMenu* menu2 = new QMenu("编辑 (&E)");
    QMenu* menu3 = new QMenu("视图 (&V)");
    QMenu* menu4 = new QMenu("关于 (&A)");
    menuBar->addMenu(menu1);
    menuBar->addMenu(menu2);
    menuBar->addMenu(menu3);
    menuBar->addMenu(menu4);

    // 创建子菜单
    QMenu* menuchild = new QMenu("写入 (&Y )");
    menu2->addMenu(menuchild);
    QAction* action6 = new QAction("换行 (&Q)");
    QAction* action7 = new QAction("键入 (&W)");
    menuchild->addAction(action6);
    menuchild->addAction(action7);

    // 给菜单添加菜单项
    QAction* action1 = new QAction("新建 (&Q)");
    QAction* action2 = new QAction("打开 (&W)");
    QAction* action3 = new QAction("保存 (&S)");
    QAction* action4 = new QAction("另存为 (&X)");
    QAction* action5 = new QAction("退出 (&E)");
    menu1->addAction(action1);
    menu1->addAction(action2);
    menu1->addSeparator();
    menu1->addAction(action3);
    menu1->addAction(action4);
    menu1->addSeparator();
    menu1->addAction(action5);

    // 给action 添加信号槽
    connect(action1, &QAction::triggered, this, &MainWindow::handle);
    connect(action5, &QAction::triggered, this, &MainWindow::close);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handle()
{
    qDebug() << "触发新建操作";
}

